package com.logan.jokebuddy;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


public class JokeBuddyActivity extends Activity {
	
	int item;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		//when the activity is called
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		item=0;
		
		//get the string from resource and display the first element
		final String jokes[]=getResources().getStringArray(R.array.jokes);
	    final TextView tv=(TextView)findViewById(R.id.mainTextView);
	    tv.setText(jokes[item]);
	
	    //react to the 'Next' button click
	    //don't allow to go out of jokes array bounds
	    ImageButton btn_next=(ImageButton)findViewById(R.id.button_next);
	    btn_next.setOnClickListener(new View.OnClickListener() {

	    	@Override
	    	public void onClick(View v) {
	    		item++;
	    		if(item >= jokes.length) {
	    			Toast toast = Toast.makeText(getApplicationContext(),
	    					R.string.no_more, Toast.LENGTH_SHORT);
	    			toast.show();
	    			item--;
	    		}
	    		else
	    			tv.setText(jokes[item]);
	    	}
	    });
	    
	    //react to the 'Previous' button click
	    //don't allow to go out of jokes array bounds
	    ImageButton btn_previous=(ImageButton)findViewById(R.id.button_previous);
	    btn_previous.setOnClickListener(new View.OnClickListener() {

	    	@Override
	    	public void onClick(View v) {
	    		item--; 
	    		if(item < 0) {
	    			Toast toast = Toast.makeText(getApplicationContext(),
	    					R.string.jokes_beginning, Toast.LENGTH_SHORT);
	    			toast.show();
	    			item++;
	    		}
	    		else
	    			tv.setText(jokes[item]);
	    	}
	    });
	}//onCreate
	
	
	public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        return true;
    }//onCreateOptionsMenu
 
    
    //React to each menu item selected
    //Identify single menu item by it's id
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
 
        switch (item.getItemId()) {
        case R.id.menu_about:
            //Create and show dialog from popup.xml
        	Dialog dialog = new Dialog(JokeBuddyActivity.this);
        	dialog.setContentView(R.layout.popup);
        	dialog.setTitle("About JokeBuddy");
        	TextView text = (TextView) dialog.findViewById(R.id.text);
        	text.setText(R.string.about);
        	
        	dialog.show();
        	return true;
 
        case R.id.menu_exit:
            //exit
            finish();
            return true;
 
        default:
            return super.onOptionsItemSelected(item);
        }
    }//onOptionsItemSelected  
}